require("../less/index.less")

document.ready(function() {
    window.footerFn('index');

    //轮播图
    var mySwiper = new Swiper('.swiper-container', {
        // direction: 'vertical', // 垂直切换选项
        loop: true, // 循环模式选项

        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        },
    })


    dataRender();
    //首页数据动态渲染

    let rankingCount = document.querySelector('#rankingCount');
    let clockCount = document.querySelector('#clockCount');
    let medalCount = document.querySelector('#medalCount');
    let isPunch = document.querySelector('#isPunch');

    //写到函数里面可以避免数据污染
    function dataRender() {
        axios.get("http://139.9.177.51:8099/headPageInfo?userId=" + localStorage.getItem("userId"))
            .then(function(res) {
                rankingCount.textContent = res.data.data.rank;
                clockCount.textContent = res.data.data.punchIn;
                medalCount.textContent = res.data.data.insigniaNum;
                if (res.data.data.isPunch == 'true') {
                    isPunch.textContent = '已打卡';
                    isPunch.style.opacity = 0.5;
                }
            })
            .catch(function(err) {
                console.log(err);
            })
    }

    //打卡
    isPunch.addEventListener('click', function() {
        //只要向打卡界面发出了请求,并访问到了,就算打卡了
        axios.get(`http://139.9.177.51:8099/clockIn?userId=${localStorage.getItem("userId")}`)
            .then(function(res) {
                if (res.data.status == 0) {
                    window.tipsFn('打卡成功', res.data.status);
                    dataRender();
                } else {
                    window.tipsFn('请明日再来', res.data.status);
                }

            })
            .catch(function(err) {
                console.log(err);
            })

    })
    document.querySelector('.movement').addEventListener('click', function() {
        location.href = './sportsData.html';
    })
    document.querySelector('.medal').addEventListener('click', function() {
        location.href = './myBadge.html';
    })
    document.querySelector('.course').addEventListener('click', function() {
        location.href = './training.html';
    })
    document.querySelector('.outdoors').addEventListener('click', function() {
        location.href = './running.html';
    })




})